<template>
	<div class="app-container">
		<el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" size="small" v-show="showSearch">

			<el-form-item label="节点" prop="nodeKey">
				<el-input
					@keyup.enter.native="handleQuery"
					clearable
					placeholder="请输入节点"
					v-model="queryParams.nodeKey"/>
			</el-form-item>

			<el-form-item>
				<el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
				<el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">

			<el-col :span="1.5">
				<el-button
					@click="handleExport"
					icon="el-icon-download"
					plain
					size="mini"
					type="warning"
					v-hasPermi="['workflow:bpmnNode:export']">导出</el-button>
			</el-col>

			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table :data="bpmnNodeList" v-loading="loading">
			<el-table-column align="center" label="主键" prop="id"/>
			<el-table-column align="center" label="流程信息" prop="bpmnInfoId"/>
			<el-table-column align="center" label="节点 Key" prop="nodeKey"/>
			<el-table-column align="center" label="节点描述" prop="nodeDesc" show-overflow-tooltip/>

			<el-table-column align="center" class-name="small-padding fixed-width" label="操作">
				<template slot-scope="scope">
					<el-button
						@click="handleUpdate(scope.row)"
						icon="el-icon-edit"
						size="mini"
						type="text"
						v-hasPermi="['workflow:bpmnNode:edit']">修改</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 修改流程定义节点对话框 -->
		<el-dialog title="修改流程定义节点" :visible.sync="open" append-to-body width="500px">
			<el-form :model="form" :rules="rules" label-width="80px" ref="form">
				<el-form-item label="节点 Key" prop="nodeKey">
					<el-input placeholder="请输入节点" v-model="form.nodeKey" disabled/>
				</el-form-item>
				<el-form-item label="节点描述" prop="nodeDesc">
					<el-input placeholder="请输入节点描述" v-model="form.nodeDesc"/>
				</el-form-item>

				<el-form-item label="审核人员" prop="auditors">
					<el-select v-model="form.auditors" multiple filterable>
						<el-option v-for="u in users" :value="u.userId" :label="u.nickName || u.userName"/>
					</el-select>
				</el-form-item>

			</el-form>
			<div class="dialog-footer" slot="footer">
				<el-button @click="submitForm" type="primary">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {getBpmnNode, listBpmnNode, updateBpmnNode} from "@/api/workflow/bpmnNode";
	import {listAllUser} from "@/api/system/user";

	export default {
		name: "BpmnNode",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 显示搜索条件
				showSearch: true,
				// 流程定义节点表格数据
				bpmnNodeList: [],
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					nodeKey: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				// 所有用户列表
				users: [],
			};
		},
		created() {
			// 基于流程信息 id 查询流程节点列表
			// 基于 path(路径) 跳转去 query  里查看参数
			// 基于 name(名字) 跳转去 params 里查看参数
			this.queryParams.bpmnInfoId = this.$route.query.id

			this.getList();

			// 查询所有用户信息
			listAllUser().then(res => {
				this.users = res.data
			})
		},
		methods: {
			/** 查询流程定义节点列表 */
			getList() {
				this.loading = true;
				listBpmnNode(this.queryParams).then(response => {
					this.bpmnNodeList = response.data;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					bpmnInfoId: null,
					nodeKey: null,
					nodeDesc: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getBpmnNode(id).then(response => {
					this.form = response.data;
					this.open = true;
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						// 提交之前, 获取到当前页面的 bpmnInfoId 设置到表单中
						this.form.bpmnInfoId = this.$route.query.id

						updateBpmnNode(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					}
				});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('workflow/bpmnNode/export', {
					...this.queryParams
				}, `bpmnNode_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
